/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package co.edu.senaclem.persistencia;

import co.edu.senaclem.modelo.Instructor;
import co.edu.senaclem.modelo.Personal;
import static co.edu.senaclem.persistencia.Persistencia.areadb;
import static co.edu.senaclem.persistencia.Persistencia.con;
import static co.edu.senaclem.persistencia.Persistencia.conectar;
import static co.edu.senaclem.persistencia.Persistencia.desconectar;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;

/**
 *
 * @author anfeles
 */
@Stateless
public class InstructorDAO implements IInstructorDAO{
    
    public static ResultSet resSet=null;
    
    @Override
    public void eliminar(Instructor instructor) throws Exception {
        try {
            if (conectar()) {
                String consulta = "delete from instructor where documentoinstructor='" +instructor.getDocumento().getDocumento()+"'";
                areadb = con.createStatement();
                areadb.execute(consulta);

                desconectar();
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void modificar(Instructor instructor) throws Exception {
        try {
            if (conectar()) {
                String consulta = "update instructor set tipoinstructor='" + instructor.getTipo()+ "' where documentoinstructor='"+instructor.getDocumento().getDocumento()+"'";
                areadb = con.createStatement();
                areadb.execute(consulta);

                desconectar();
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public void insertar(Instructor instructor) throws Exception {
        try {
            if (conectar()) {
                String consulta = "insert into instructor values('" + instructor.getDocumento().getDocumento()+ "','" + instructor.getTipo()+ "')";
                areadb = con.createStatement();
                areadb.execute(consulta);

                desconectar();
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public Instructor consultarPorId(Long documento) throws Exception {
        try {
            Instructor miInstructor = null;
            if (conectar()) {
                String consulta = "Select * From instructor where documentoinstructor='" + documento + "'";
                areadb = con.createStatement();
                resSet = areadb.executeQuery(consulta);

                if (resSet.next()) {
                    miInstructor = new Instructor();
                    IPersonalDAO personalDAO=new PersonalDAO();
                    miInstructor.setTipo(resSet.getString("tipoinstructor"));
                    Personal miPersonal=personalDAO.consultarPorId(resSet.getLong("documentoinstructor"));                                        
                    miInstructor.setDocumento(miPersonal);                    
                }

                resSet.close();
                desconectar();
            }

            return miInstructor;
        } catch (RuntimeException e) {
            throw e;
        }
    }

    @Override
    public List<Instructor> consultar() throws Exception {
        try {
            List<Instructor> instructores = new ArrayList<Instructor>();
            if (conectar()) {
                String consulta = "Select * From instructor";
                areadb = con.createStatement();
                resSet = areadb.executeQuery(consulta);

                while (resSet.next()) {
                    Instructor miInstructor = new Instructor();
                   
                    //se consulta el personal asociado al instructor
                    IPersonalDAO personalDAO=new PersonalDAO();
                    Personal miPersonal=personalDAO.consultarPorId(resSet.getLong("documentoinstructor"));                                        
                    miInstructor.setDocumento(miPersonal); 
                    miInstructor.setTipo(resSet.getString("tipoinstructor"));

                    instructores.add(miInstructor);
                }

                resSet.close();
                desconectar();
            }

            return instructores;
        } catch (RuntimeException e) {
            throw e;
        }
    }
    
}
